﻿using Microsoft.AspNetCore.Mvc;
using RuoVea.ExDto;
using RuoVea.ExEnum;
using RuoVea.ExSugar.Entity;
using RuoVea.OmiApi.Article.Service;
using RuoVea.OmiApi.Article.Service.Dto;
using RuoVea.OmiApi.Upload.Service;
using RuoVea.OmiArticle.Attributes;
using RuoVea.OmiArticle.Language;

namespace RuoVea.OmiArticle.Controllers;

/// <summary>
/// 文章管理
/// </summary>
[AppRoute]
public class ArticleController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly ArticleService _thisService;
    private readonly FileUploadService _fileUploadService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="thisService"></param>
    /// <param name="fileUploadService"></param>
    public ArticleController(ArticleService thisService, FileUploadService fileUploadService)
    {
        _thisService = thisService;
        _fileUploadService = fileUploadService;
    }

    /// <summary>
    /// 页面
    /// </summary>
    [HttpGet(nameof(Index))]
    public IActionResult Index()
    {
        return View();
    }
    /// <summary>
    /// 页面
    /// </summary>
    [HttpGet(nameof(Edit))]
    [HttpGet(nameof(Edit) + "/{id}")]
    public IActionResult Edit(long? id)
    {
        ViewBag.Id = id;
        return View();
    }
    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<ArtArticleOutDto>>> GetPagesAsync(ArticleParam data)
    {
        RestfulResult<PageResult<ArtArticleOutDto>> restfulResult = new() { Code = CodeStatus.OK };
        restfulResult.Data = await _thisService.GetPagesAsync(data);
        return restfulResult;
    }

    /// <summary>
    /// 根据主键查询🔹
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpGet("data")]
    public virtual async Task<RestfulResult<ArtArticleOutDto>> GetDataAsync([FromQuery] EntityBaseId data)
    {
        RestfulResult<ArtArticleOutDto> restfulResult = new() { Code = CodeStatus.OK };
        restfulResult.Data = await _thisService.GetDataAsync(data);
        return restfulResult;
    }
    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("add")]
    public async Task<RestfulResult> AddAsync(ArticleDto data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        if (data == null) return new RestfulResult { Code = CodeStatus.BadRequest, Message = i18n.no_data_obtained };
        try
        {
            await _thisService.AddDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("update")]
    public async Task<RestfulResult> UpdateAsync(ArticleDto data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        if (data == null) return new RestfulResult { Code = CodeStatus.BadRequest, Message = i18n.no_data_obtained };
        try
        {
            await _thisService.UpdateDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task<RestfulResult> DeleteDataAsync(EntityBaseId data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        if (data == null) return new RestfulResult { Code = CodeStatus.BadRequest, Message = i18n.no_data_obtained };
        try
        {
            await _thisService.DeleteDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Message = ex.Message;
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            return restfulResult;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    [HttpPost("upload")]
    public async Task<RestfulResult> Upload(IFormFile file)
    {
        return await _fileUploadService.Image(file);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    [HttpPost("upload-image")]
    public async Task<RestfulResult> UploadImage(IFormFile file)
    {
        return await _fileUploadService.Image(file);
    }
}